package com.aiti.lulian.controller;

import cn.hutool.core.exceptions.ExceptionUtil;
import com.aiti.base.core.constant.MessageCodeConstant;
import com.aiti.base.core.exception.BusinessException;
import com.aiti.base.core.page.PageResult;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.gxmh.dto.GxmhCustomerDetailParamDto;
import com.aiti.lulian.gxmh.dto.GxmhCustomerPageParamDto;
import com.aiti.lulian.gxmh.service.impl.GxmhCustomerService;
import com.aiti.lulian.gxmh.vo.GxmhCustomerDetailVo;
import com.aiti.lulian.gxmh.vo.GxmhCustomerVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * description:
 * auth fcb
 * date 2025-06-30 16:43
 **/
@RestController
@RequestMapping("/gxmhCustomer")
@Slf4j
@Api(value = "工信门户--客户管理", tags = "工信门户--客户管理")
public class GxmhCustomerController {

    @Autowired
    private GxmhCustomerService gxmhCustomerService;

    @PostMapping("/queryCustomerList")
    @ApiOperation(value = "查询客户列表")
    public Message queryCustomerList(@RequestBody GxmhCustomerPageParamDto dto) {
        try {
            PageResult<GxmhCustomerVo> gxmhCustomerVoPageResult = gxmhCustomerService.queryCustomerList(dto);
            return Message.success(gxmhCustomerVoPageResult);
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("查询异常");
        }
    }

    @GetMapping("/queryCustomerDetail")
    @ApiOperation(value = "查询客户详情")
    public Message queryCustomerDetail(@Param("baseId") String baseId) {
        try {
            GxmhCustomerDetailVo gxmhCustomerDetailVo = gxmhCustomerService.queryCustomerDetail(baseId);
            return Message.success(gxmhCustomerDetailVo);
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("查询异常");
        }
    }

    @GetMapping("/queryCustomerContactsList")
    @ApiOperation(value = "查询客户联系人列表")
    public Message queryCustomerContactsList(@Param("baseId") String baseId) {
        try {
            return Message.success(gxmhCustomerService.queryCustomerContactsList(baseId));
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("查询异常");
        }
    }

    @PutMapping("/updateCustomerFollowStatus")
    @ApiOperation(value = "更新客户跟进状态")
    public Message updateCustomerFollowStatus(@RequestBody GxmhCustomerDetailParamDto dto) {
        try {
            gxmhCustomerService.updateFollowStatus(dto.getBaseId(), dto.getFollowStatus(), dto.getUserId());
            return Message.success();
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("更新异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("更新异常");
        }
    }

    @PutMapping("/updateCustomer")
    @ApiOperation(value = "更新客户")
    public Message updateCustomer(@RequestBody GxmhCustomerDetailParamDto dto) {
        try {
            gxmhCustomerService.updateCustomer(dto);
            return Message.success();
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("更新异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("更新异常");
        }
    }

    @GetMapping("/queryCustomerFollowUpRecordList")
    @ApiOperation(value = "查询客户跟进记录列表")
    public Message queryCustomerFollowUpRecordList(@Param("baseId") String baseId) {
        try {
            return Message.success(gxmhCustomerService.queryCustomerFollowUpRecordList(baseId));
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("查询异常");
        }
    }


}
